
//
//  CZPictureView.m
//  XinLang_OC
//
//  Created by WONG on 16/3/15.
//  Copyright © 2016年 cheuhui. All rights reserved.
//

#import "CZPictureView.h"
#import "CZStatus.h"
#import "CZPictureViewCell.h"

@interface CZPictureView ()

/**
 *  流水布局
 */
@property (nonatomic,strong) UICollectionViewFlowLayout *flowLayout;

@end

@implementation CZPictureView

static NSString *ReuseIdentifier = @"CZPictureViewCell";
- (instancetype)init {
    self.backgroundColor = [UIColor clearColor];
    // 注册cell
    [self registerClass:[CZPictureViewCell class] forCellWithReuseIdentifier:ReuseIdentifier];
    
    self.dataSource = self;
    
    return [super initWithFrame:CGRectZero collectionViewLayout:self.flowLayout];
}

- (void)setStatus:(CZStatus *)status {
    _status = status;
    [self reloadData];
}

- (CGSize)calcViewSize {
    // 间距
    CGFloat margin = 10;
    // 设置itemSize大小
    CGSize itemSize = CGSizeMake(90, 90);
    self.flowLayout.itemSize = itemSize;
    
    // 流水布局的间距，图片数量小于0时，没有间距
    self.flowLayout.minimumInteritemSpacing = 0;
    self.flowLayout.minimumLineSpacing = 0;
    
    // 获取图片数量
    NSUInteger count = self.status.pic_urls.count;
    
    if (count == 0) {
        return CGSizeZero;
    }
    if (count == 1) {
        CGSize size = CGSizeMake(150, 120);
        
        // 获取图片URL的地址
        NSString *strURL = [self.status.pictureURLs[0] absoluteString];
        
        // 使用SDWebImage找到这张图片, key: 是图片的url地址
        // 图片并不一定能够获取,判断是否有图片
        UIImage *image = [[SDWebImageManager sharedManager].imageCache imageFromDiskCacheForKey:strURL];
        if (image) {
            size = image.size;
        }
        
        if (size.width < 40) {
            size.width = 40;
        }
        if (size.height < 40) {
            size.height = 40;
        }
        self.flowLayout.itemSize = size;
        
        return size;
    }
    // 当图片的数量大于1时 间距为10
    self.flowLayout.minimumInteritemSpacing = 10;
    self.flowLayout.minimumLineSpacing = 10;
    
    if (count == 4) {
        CGFloat width = 2 * itemSize.width + margin;
        return CGSizeMake(width, width);
    }
    // 当count = 2，3，5，6，7，8，9
    // 列数
    int column = 3;
    // 行数：公式: 行数 = (count + column - 1) / column   注意:count, column必须是Int类型
    NSInteger row = (count + column - 1) / column;
    // 宽度：列数 * itemSize.width + (列数 - 1) * 间距
    CGFloat width = (CGFloat)column * itemSize.width + ((CGFloat)column - 1) * margin;
    // 高度: 行数 * itemSize.width + (行数 - 1) * 间距
    CGFloat height = (CGFloat)row * itemSize.width + ((CGFloat)row - 1) * margin;
    
    return CGSizeMake(width, height);
}

#pragma mark - UICollectionViewDataSource

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.status.pic_urls.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    CZPictureViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ReuseIdentifier forIndexPath:indexPath];
    
    cell.imageURL = self.status.pictureURLs[indexPath.item];
    
    return cell;
}

#pragma mark - lazy load

- (UICollectionViewFlowLayout *)flowLayout {
    if (!_flowLayout) {
        _flowLayout = [[UICollectionViewFlowLayout alloc] init];
    }
    return _flowLayout;
}

@end




















